package com.ht.right.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ht.right.entity.Dept;
import com.ht.right.entity.DeptToUser;
import com.ht.right.entity.Role;
import com.ht.right.entity.User;
import com.ht.right.entity.UserToRole;
import com.ht.right.service.DeptService;
import com.ht.right.service.DeptToUserService;
import com.ht.right.service.OrgFacadeService;
import com.ht.right.service.RoleService;
import com.ht.right.service.UserService;
import com.ht.right.service.UserToRoleService;
import com.ht.right.util.CommonUtil;

/**
 * 组织外观模型服务层
 * 
 * @author 庞成祥
 * 
 */
public class OrgFacadeServiceImpl implements OrgFacadeService {

	private DeptService deptService;
	private UserService userService;
	private DeptToUserService deptToUserService;
	private RoleService roleService;
	private UserToRoleService userToRoleService;

	// -----------------部门用户关联操作开始-------------------

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public UserToRoleService getUserToRoleService() {
		return userToRoleService;
	}

	public void setUserToRoleService(UserToRoleService userToRoleService) {
		this.userToRoleService = userToRoleService;
	}

	@Override
	public Map<String, Object> login(User user) {
		Map<String, Object> map = null;
		User u = userService.userLogin(user);
		if (CommonUtil.isNullOrEmpty(u)) {
			return null;
		} else {
			map = new HashMap<String, Object>();
			map.put("user", u);
			Set<DeptToUser> s = u.getDeptToUsers();
			StringBuffer sb = new StringBuffer(u.getUuid());
			for (DeptToUser dt : s) {
				sb.append(",");
				sb.append(dt.getDept().getUuid());
			}
			Set<UserToRole> s1 = u.getUserToRoles();
			for (UserToRole ut : s1) {
				sb.append(",");
				sb.append(ut.getRole().getUuid());
			}
			map.put("orgIds", sb.toString());
		}
		return map;
	}

	/**
	 * 添加员工,总共有如下三步 1、根据jsp传人员工信息及所选部门id，将user保存 2、通过部门id查找出相应的部门，及该部门对应的所有父节点部门
	 * 3、按照List<Dept> - user的关系生成 用户->部门 的多条数据
	 * 
	 * @return
	 */
	@Override
	public void addUser(User user, String sameLevelNodeId) {
		int tempNo = 0;
		if (CommonUtil.isNullOrEmpty(sameLevelNodeId)) {
			Object obj = userService.findMaxUserPosition(user);
			if (null == obj) {
			} else {
				tempNo = Integer.parseInt(obj.toString()) + 1;
			}
		} else {
			User laterUser = userService.findUserById(sameLevelNodeId);
			tempNo = laterUser.getUserPostion();
			userService.updateLaterUserPosition(laterUser);
		}
		user.setUserPostion(tempNo);
		userService.saveUser(user);
		// -----------------------------------------
		String deptId = user.getUserDeptId();
		List<Dept> list = new ArrayList<Dept>();
		if (CommonUtil.isNullOrEmpty(deptId)) {
		} else {
			Dept d = deptService.findDeptById(deptId);
			list.add(d);
			while ("-1".equals(d.getDeptPid()) == false) {
				d = deptService.findDeptById(d.getDeptPid());
				list.add(d);
			}
		}
		// ----------------------------------------
		DeptToUser deptToUser = null;
		for (Dept d : list) {
			deptToUser = new DeptToUser();
			deptToUser.setDept(d);
			deptToUser.setUser(user);
			deptToUser.setDeptRole("1");
			deptToUser.setIsParttimeDept("1");
			deptToUser.setCreateBy("admin");
			deptToUser.setCreateDate(new Date());
			deptToUser.setUpdateBy("admin");
			deptToUser.setUpdateDate(new Date());
			deptToUser.setDelFlag("1");
			deptToUserService.saveDeptToUser(deptToUser);
		}
		// -----------向orgMap映射表中添加记录-------
		/*
		 * OrgMap orgMap = new OrgMap(); orgMap.setOrgId(user.getUserId());
		 * //1、用户 2、部门 3、角色 orgMap.setOrgTypt(1); orgMap.setCreateBy("admin");
		 * orgMap.setCreateDate(new Date()); orgMap.setUpdateBy("admin");
		 * orgMap.setUpdateDate(new Date()); orgMap.setDelFlag("1");
		 * orgMapService.addOrgMap(orgMap);
		 */
	}

	// 删除部门级联删除用户
	@Override
	public void deleteDept(String deptId) {
		// 删除部门信息
		deptService.deleteDeptById(deptId);
		// 删除用户信息
		userService.deleteUserByDeptId(deptId);
	}

	// 创建组织机构树
	@Override
	public List<Map<String, String>> findOrgTree() {
		List<User> userList = userService.findAllList();
		Map<String, List<Map<String, String>>> map = new HashMap<String, List<Map<String, String>>>();
		Map<String, String> subMap = null;
		for (User user : userList) {
			subMap = new HashMap<String, String>();
			subMap.put("id", user.getUuid());
			subMap.put("text", user.getUserName());
			subMap.put("pid", user.getUserDeptId());
			subMap.put("type", "user");
			if (map.containsKey(user.getUserDeptId())) {
				map.get(user.getUserDeptId()).add(subMap);
			} else {
				List<Map<String, String>> t = new ArrayList<Map<String, String>>();
				t.add(subMap);
				map.put(user.getUserDeptId(), t);
			}
		}

		List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
		List<Dept> deptList = deptService.findAllDeptList();
		for (Dept d : deptList) {
			Map<String, String> m = new HashMap<String, String>();
			m.put("id", d.getUuid());
			m.put("text", d.getDeptName());
			m.put("pid", d.getDeptPid());
			m.put("type", "dept");
			dataList.add(m);
			if (map.containsKey(d.getUuid())) {
				dataList.addAll(map.get(d.getUuid()));
			}
		}
		return dataList;
	}

	// -----------------部门用户关联操作结束-------------------

	// -----------------角色用户关联操作开始-------------------

	@SuppressWarnings("null")
	public void saveUserToRole(Role role, String userList) {
		roleService.saveRole(role);
		if (!(userList == null && userList.equals(""))) {
			String[] id = userList.split(",");
			for (int i = 0; i < id.length; i++) {
				User user = new User();
				user.setUuid(id[i]);
				UserToRole userToRole = new UserToRole();
				userToRole.setUser(user);
				userToRole.setRole(role);
				userToRole.setRoleType("1");
				userToRole.setCreateBy("admin");
				userToRole.setCreateDate(new Date());
				userToRole.setUpdateBy("admin");
				userToRole.setUpdateDate(new Date());
				userToRole.setDelFlag("1");
				userToRoleService.saveUserToRole(userToRole);
			}
		}
	}

	public List<Map<String, String>> findUserIdByRole(Role role) {
		UserToRole userToRole = new UserToRole();
		Map<String, String> map = null;
		userToRole.setRole(role);
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		List<UserToRole> listUser = userToRoleService.findUserIdByRole(userToRole);
		for (UserToRole user : listUser) {
			map = new HashMap<String, String>();
			map.put("id", user.getUser().getUuid());
			map.put("text", user.getUser().getUserName());
			list.add(map);
		}
		return list;
	}

	// 更新角色信息
	public void updateUserToRole(Role role, UserToRole userToRole, String addUserList) {
		roleService.updateRole(role);
		List<UserToRole> listUser = userToRoleService.findUserIdByRole(userToRole);
		for (UserToRole userRole : listUser) {
			userToRoleService.deleteUserToRole(userRole);
		}
		String[] ids = addUserList.split(",");
		for (int i = 0; i < ids.length; i++) {
			User user = new User();
			user.setUuid(ids[i]);
			UserToRole newUserToRole = new UserToRole();
			newUserToRole.setUser(user);
			newUserToRole.setRole(role);
			newUserToRole.setRoleType("1");
			newUserToRole.setCreateBy("admin2");
			newUserToRole.setCreateDate(new Date());
			newUserToRole.setUpdateBy("admin2");
			newUserToRole.setUpdateDate(new Date());
			newUserToRole.setDelFlag("1");
			userToRoleService.saveUserToRole(newUserToRole);
		}
	}

	// -----------------角色用户关联操作结束-------------------

	public DeptToUserService getDeptToUserService() {
		return deptToUserService;
	}

	public void setDeptToUserService(DeptToUserService deptToUserService) {
		this.deptToUserService = deptToUserService;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
